package com.bugreport.integration.web;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.JavaScriptPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class ObtainJSLibraryTestCase extends JettyServerTestCase {

	private String jQueryLibraryUrl = getJettyBaseUrl() + "ressources/jquery-1.7.1.min.js";
	private String jQueryUiLibraryUrl = getJettyBaseUrl() + "ressources/jquery-ui-1.7.3.custom.min.js";
	private String jQueryHtmlTestPage = getJettyBaseUrl() + "testJQuery.html";

	@Test
	public void getJQueryLibrary() throws Exception {

		// Given
		final WebClient webClient = new WebClient();
		WebRequest request = new WebRequest(new URL(jQueryLibraryUrl), HttpMethod.GET);

		// When
		JavaScriptPage page = webClient.getPage(request);

		// Then
		assertNotNull(page);
		assertTrue(page.getContent().contains("jQuery v1.7.1 jquery.com"));
	}

	@Test
	public void getJQueryUiLibrary() throws Exception {

		// Given
		final WebClient webClient = new WebClient();
		WebRequest request = new WebRequest(new URL(jQueryUiLibraryUrl), HttpMethod.GET);

		// When
		JavaScriptPage page = webClient.getPage(request);

		// Then
		assertNotNull(page);
		assertTrue(page.getContent().contains("jQuery UI 1.7.3"));
	}

	@Test
	public void getBugReportJSEntryPoint() throws Exception {

		// Given
		final WebClient webClient = new WebClient();
		WebRequest request = new WebRequest(new URL(getJettyBaseUrl() + "ressources/bugreport.js"), HttpMethod.GET);

		// When
		JavaScriptPage page = webClient.getPage(request);

		// Then
		assertNotNull(page);
		assertTrue(page.getContent().contains("IncludeJavaScript('" + jQueryLibraryUrl + "');"));
		assertTrue(page.getContent().contains("IncludeJavaScript('" + jQueryUiLibraryUrl + "');"));
	}

	@Test
	public void getBugReportJSEntryPointWithJQuery() throws Exception {

		// Given
		final WebClient webClient = new WebClient();
		WebRequest request = new WebRequest(new URL(getJettyBaseUrl() + "ressources/bugreport.js?includeJQuery=true"),
				HttpMethod.GET);

		// When
		JavaScriptPage page = webClient.getPage(request);

		// Then
		assertNotNull(page);
		assertTrue(page.getContent().contains("IncludeJavaScript('" + jQueryLibraryUrl + "');"));
		assertTrue(page.getContent().contains("IncludeJavaScript('" + jQueryUiLibraryUrl + "');"));
	}

	@Test
	public void getBugReportJSEntryPointWithoutJQuery() throws Exception {

		// Given
		final WebClient webClient = new WebClient();
		WebRequest request = new WebRequest(new URL(getJettyBaseUrl() + "ressources/bugreport.js?includeJQuery=false"),
				HttpMethod.GET);

		// When
		JavaScriptPage page = webClient.getPage(request);

		// Then
		assertNotNull(page);
		assertFalse(page.getContent().contains("IncludeJavaScript('" + jQueryLibraryUrl + "');"));
		assertFalse(page.getContent().contains("IncludeJavaScript('" + jQueryUiLibraryUrl + "');"));
	}

	@Test
	public void verifyJQueryIsWellExecutableUsingBugReportEntryPoint() throws Exception {

		// Given
		final WebClient webClient = new WebClient();

		final List<String> expectedAlerts = Collections.singletonList("success");
		final List<String> collectedAlerts = new ArrayList<String>();
		webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

		// When
		HtmlPage page = webClient.getPage(jQueryHtmlTestPage);

		// Then
		assertNotNull(page);
		assertEquals(expectedAlerts, collectedAlerts);
		assertTrue(page.getBody().asXml().contains("success"));
	}
}
